﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using assignment_1.Models;

namespace assignment_1.Controllers
{
    public class HomeController : Controller
    {
        DataRepository rep = new DataRepository();
        public ActionResult Index()
        {
            var canv = UnsavedCanvas.GetCanvas(this.HttpContext);
            if (User.Identity.IsAuthenticated)
            {
                var canvas = canv.GetCanvasItems();
                canv.EmptyCanvas();
                if (canvas != null)
                {
                    var shape = new Canva();
                    String strUser = User.Identity.Name;
                    if (!String.IsNullOrEmpty(strUser))
                    {
                        int slashPos = strUser.IndexOf("\\");
                        if (slashPos != -1)
                        {
                            strUser = strUser.Substring(slashPos + 1);
                        }
                        shape.userName = strUser;
                    }              
                    shape.canvasName = canvas.unsavedCanvasName;
                    shape.canvasShapes = canvas.unsavedCanvasShapes;
                    shape.canvasDate = canvas.unsavedCanvasDate;
                    rep.SaveUserCanvas(shape);
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult SaveCanvas(String TheCanvas, String TheCanvasName)
        {
            var canv = UnsavedCanvas.GetCanvas(this.HttpContext);

            Canva canvas = new Canva();
            canvas.canvasShapes = TheCanvas;
            canvas.canvasName = TheCanvasName;
            canvas.canvasDate = DateTime.Now;
            String strUser = User.Identity.Name;
            if (!String.IsNullOrEmpty(strUser))
            {
                int slashPos = strUser.IndexOf("\\");
                if (slashPos != -1)
                {
                    strUser = strUser.Substring(slashPos + 1);
                }
                canvas.userName = strUser;
                rep.SaveUserCanvas(canvas);
            }
            else
            {
                canvas.userName = "Unknown user";
                canv.AddToCanvas(canvas);
                return Json(0, JsonRequestBehavior.AllowGet);
            }

            var shapes = rep.getCanvasByUserName(canvas.userName);
            var newShapes = from item in shapes
                           select new { Name = item.canvasName, TheShapes = item.canvasShapes, ID = item.canvasID, Date = item.canvasDate.ToString() };
            return Json(newShapes, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DeleteCanvas(int? canvasId)
        {
            if (canvasId.HasValue)
            {
                int realId = canvasId.Value;
                var canvas = rep.getSingleCanvasByUd(realId);
                rep.deleteCanvas(realId);
                var shapes = rep.getCanvasByUserName(canvas.userName);
                var newShapes = from item in shapes
                                select new { Name = item.canvasName, TheShapes = item.canvasShapes, ID = item.canvasID, Date = item.canvasDate.ToString() };
                return Json(newShapes, JsonRequestBehavior.AllowGet);
            }
            return Json(0, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetShapes(int? canvasId)
        {
            if(canvasId.HasValue)
            {
                int realId = canvasId.Value;
                var shapes = rep.getCanvasByID(realId);
                var newShapes = from item in shapes
                                select new { Name = item.canvasName, TheShapes = item.canvasShapes, ID = item.canvasID, Date = item.canvasDate.ToString() };
                return Json(newShapes, JsonRequestBehavior.AllowGet);
            }
            return Json(0, JsonRequestBehavior.AllowGet);
        }

        [ChildActionOnly]
        public ActionResult CanvasList()
        {
            IEnumerable<Canva> result;
            String strUser = User.Identity.Name;
            if (!String.IsNullOrEmpty(strUser))
            {
                int slashPos = strUser.IndexOf("\\");
                if (slashPos != -1)
                {
                    strUser = strUser.Substring(slashPos + 1);
                }
                result = rep.getCanvasByUserName(strUser);
            }
            else
            {
                result = null;
            }
            return View(result);
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
